package com.corpgovernment.common.pay;

import com.corpgovernment.common.supplier.SHA256Utils;
import com.corpgovernment.common.utils.BaseUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class PPayUtil {
    /**
     * 创建支付签名
     * @param orderId
     * @param rebookId
     * @param guid
     * @param time
     * @return
     */
    public static String createSign(String orderId, String rebookId,String guid,String time){
        String key="pPay%";
        String signString=String.format("%s%s%s%s%s",key,orderId,rebookId,guid,time);
        log.info("signString:{}",signString);

        return SHA256Utils.getSHA256StrJava(signString);
    }
    /**
     * 创建签名信息
     * @return
     */
    public static Map<String,String> createSignInfo(String orderId, String rebookId){
        Map<String,String> map=new HashMap<>(5);
        String guid=BaseUtils.getUUID();
        String time=BaseUtils.getTime().toString();

        map.put("guid",guid);
        map.put("time",time);
        map.put("sign",createSign(orderId,rebookId,guid,time));

        return map;
    }

    /**
     * 校验支付签名
     * @return
     */
    public static boolean checkSign(PayTokenInfo payTokenInfo){
        String newSign = createSign(payTokenInfo.getOrderId(), payTokenInfo.getRebookId(), payTokenInfo.getGuid(), payTokenInfo.getTime());
        return payTokenInfo.getSign().equals(newSign);
    }

    /**
     * 创建跳转支付平台token数据
     * @return
     */
    public static PayTokenInfo createPayTokenInfo(PayTokenInfo payTokenInfo){
        Map<String, String> signInfoMap = createSignInfo(payTokenInfo.getOrderId(), payTokenInfo.getRebookId());

        payTokenInfo.setGuid(signInfoMap.get("guid"));
        payTokenInfo.setTime(signInfoMap.get("time"));
        payTokenInfo.setSign(signInfoMap.get("sign"));

        return payTokenInfo;
    }
}
